package jiraworkflow

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser

/**
 * Reporter设置“评审人”，点击工作流中的“开始审批”，“评审人”自动关注该Issue
 * @author caiwl* @date 2020/4/15 10:05
 */

// 获取Jira管理器
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()

// 根据编号获取Issue，上线时，当前Issue不用代码获取，所以需要注释该行
Issue issue = issueManager.getIssueObject("IPD-43")
// 根据名称获取自定义字段
//CustomField approve = customFieldManager.getCustomFieldObjectByName("评审人")
// 使用中文名获取自定义字段时，重名问题无法处理，建议使用字段ID获取
CustomField approve = customFieldManager.getCustomFieldObject("customfield_10800")
// 获取自定义字段对应的值，该方法本身返回值类型是Object，所以需要根据字段本身存储值的类型进行强转
Set<ApplicationUser> approvers = issue.getCustomFieldValue(approve) as Set<ApplicationUser>
for (ApplicationUser approver : approvers) {
    // 设置评审人一一关注当前Issue
    watcherManager.startWatching(approver, issue)
}

